package com.xiaotu.service.Impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.xiaotu.mapper.MovieMapper;
import com.xiaotu.pojo.Movie;
import com.xiaotu.pojo.MovieType;
import com.xiaotu.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class MoviceServiceImpl implements MovieService {
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<MovieType> findAll() {

        List<MovieType> all = new ArrayList<>();
        List<Movie> movies = new ArrayList<>();
        Object movieType = redisTemplate.opsForValue().get("movieType_");
        if(movieType == null){
            all = movieMapper.findAll();
            if (all.size()>0) {
                redisTemplate.opsForValue().set("movieType_", all);
            }
        }else{
            String s = JSON.toJSONString(movieType);
            all.addAll(JSON.parseArray(s,MovieType.class));
        }
        /*去缓存中查询电影相关数据*/
        Object o = redisTemplate.opsForValue().get("movies_");
        /*如果没有查到,就去数据库查询并存入缓存*/
        if (o == null) {
            movies = movieMapper.selectAll();
            if(movies.size()>0){
                redisTemplate.opsForValue().set("movies_", movies);
             /* Iterator<Movie> iterator = movies.iterator();
                while (iterator.hasNext()){
                    Movie next = iterator.next();
                    redisTemplate.opsForValue().set("movies_"+next.getId(), next);
                }*/
            }
        /*如果查到.将查到的数据转换成List  因为存储形式是JSON格式*/
        }else {
            String s = JSON.toJSONString(o);
            List<Movie> list  = JSON.parseArray(s, Movie.class);
            movies.addAll(list);
        }
        /*将电影放入对应的类型中并返回*/
        for (MovieType m : all) {
            List<Movie> movieList = new ArrayList<>();
            m.getNewMovieList();
            for (Movie movie : movies) {
                if (m.getId() == movie.getTypeId()) {
                    movieList.add(movie);
                }
            }
            m.setMovieList(movieList);
        }
        return all;
    }

    @Override
    public List<Movie> selectAll() {
        return null;
    }

    @Override
    public Movie findById(Integer id) {
       /* Object movies_ = redisTemplate.opsForValue().get("movies_");
        if (movies_!=null){
            String s = JSON.toJSONString(movies_);
            List<Movie> movieList = JSON.parseArray(s, Movie.class);
            if(movieList.size()>0){
                Movie movie = movieList.get(id);
                return movie;
            }else {
                return null;
            }
        }else {*/
            Movie byId = movieMapper.findById(id);
            return byId;
        //}
    }

    @Override
    public Movie findByTId(Integer typeId) {
        return null;
    }

    @Override
    public Integer updateById(Movie movie) {
        Integer integer = movieMapper.updateById(movie);
        return null;
    }

    @Override
    public Integer insertMovie(Movie movie) {
        return null;
    }

    @Override
    public Integer deleteById(Integer id) {
        return null;
    }
}
